import { updateNickname, updatePassword, upload } from "@/api/settingsApi";
export default {
  data() {
    return {
      admin: {},
      formNickname: {
        username: "",
        nickname: "",
      },
      formPassword: {
        username: "",
        passwordOut: "",
        password: "",
      },
      formUpload: {
        id: "",
        imgUrl: "",
        username: "",
      },
    };
  },
  methods: {
    // 饿了么 上传组件开始
    handleAvatarSuccess(res, file) {
      this.admin.imgUrl = URL.createObjectURL(file.raw);
      this.formUpload.id = this.admin.id;
      this.formUpload.username = this.admin.username;
      this.formUpload.imgUrl = res.data;
      upload(this.formUpload).then((resp) => {
        if (resp.data.msg === "success" && resp.data.code === 200) {
          this.$message.success("头像上传成功");
          // 数据回显
          this.$store.dispatch("getAdmin");
        } else {
          this.$message.error("头像上传失败");
        }
      });
    },
    // 上传图片设置
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 昵称修改方法
    submitFormNickname() {
      const nickname = this.formNickname.nickname;
      const regex = /^[\u4e00-\u9fa5]{2,6}$/;
      if (!regex.test(nickname)) {
        this.$message.error("昵称必须是2到6个字符");
        return false;
      }
      // 不能和原名字相同
      if (nickname === this.admin.nickname) {
        this.$message.error("新昵称不能和原昵称相同");
        return false;
      }
      // 这里添加axios方法
      updateNickname(this.formNickname).then((resp) => {
        if (resp.data.code === 200 && resp.data.msg === "success") {
          this.$message.success("修改成功");
          // 数据回显
          this.$store.dispatch("getAdmin");
        } else {
          this.$message.error(resp.data.msg);
        }
      });
    },

    // 密码输入框验证
    checkChinese(value) {
      const regex = /^[^\\u4e00-\\u9fa5]{6,20}$/;
      if (!regex.test(value)) {
        return false;
      }
    },
    // 密码修改方法
    submitFormPassword() {
      const passwordOut = this.formPassword.passwordOut;
      const password = this.formPassword.password;
      const regex = /^[a-zA-Z0-9]{6,20}$/;

      if (!regex.test(password) && !regex.test(passwordOut)) {
        this.$message.error("密码必须在6个到20个字符之间");
        return false;
      }
      this.formPassword.username = this.admin.username;
      // 这里添加axios方法
      updatePassword(this.formPassword).then((resp) => {
        if (resp.data.code === 200 && resp.data.msg === "success") {
          this.$message.success("修改成功,请重新登录");
          // 数据回显
          localStorage.removeItem("token");
          localStorage.removeItem("activeIndex");
          this.$router.push("/admin/login");
        } else {
          this.$message.error(resp.data.msg);
        }
      });
    },
    // 饿了么 上传组件结束
  },
  mounted() {},
  created: function () {
    this.admin = this.$store.state.admin;
    this.formNickname.username = this.$store.state.admin.username;
  },
};
